package cn.csc.module.pay.service.funddetails;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigInteger;
import java.util.*;
import cn.csc.module.pay.controller.admin.funddetails.vo.*;
import cn.csc.module.pay.dal.dataobject.funddetails.PayFundDetailsDO;
import cn.csc.framework.common.pojo.PageResult;

import cn.csc.module.pay.convert.funddetails.PayFundDetailsConvert;
import cn.csc.module.pay.dal.mysql.funddetails.PayFundDetailsMapper;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.pay.enums.ErrorCodeConstants.*;

/**
 * 用户资金收支明细 Service 实现类
 *
 * @author 华南城
 */
@Service
@Validated
public class PayFundDetailsServiceImpl implements PayFundDetailsService {

    @Resource
    private PayFundDetailsMapper fundDetailsMapper;

    @Override
    public BigInteger createFundDetails(PayFundDetailsCreateReqVO createReqVO) {
        // 插入
        PayFundDetailsDO fundDetails = PayFundDetailsConvert.INSTANCE.convert(createReqVO);
        fundDetailsMapper.insert(fundDetails);
        // 返回
        return fundDetails.getId();
    }

    @Override
    public void updateFundDetails(PayFundDetailsUpdateReqVO updateReqVO) {
        // 校验存在
        validateFundDetailsExists(updateReqVO.getId());
        // 更新
        PayFundDetailsDO updateObj = PayFundDetailsConvert.INSTANCE.convert(updateReqVO);
        fundDetailsMapper.updateById(updateObj);
    }

    @Override
    public void deleteFundDetails(BigInteger id) {
        // 校验存在
        validateFundDetailsExists(id);
        // 删除
        fundDetailsMapper.deleteById(id);
    }

    private void validateFundDetailsExists(BigInteger id) {
        if (fundDetailsMapper.selectById(id) == null) {
            throw exception(FUND_DETAILS_NOT_EXISTS);
        }
    }

    @Override
    public PayFundDetailsDO getFundDetails(BigInteger id) {
        return fundDetailsMapper.selectById(id);
    }

    @Override
    public List<PayFundDetailsDO> getFundDetailsList(Collection<BigInteger> ids) {
        return fundDetailsMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<PayFundDetailsDO> getFundDetailsPage(PayFundDetailsPageReqVO pageReqVO) {
        return fundDetailsMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PayFundDetailsDO> getFundDetailsList(PayFundDetailsExportReqVO exportReqVO) {
        return fundDetailsMapper.selectList(exportReqVO);
    }

}
